package com.aiju.cash.ui.account;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.aiju.base.activity.BaseTitleActivity;
import com.aiju.cash.R;

/**
 * @author: 佐为
 * @date: 2017-04-07 15:49
 * @version: V1.0
 * @Description: 账目备注
 */
public class AccountRemarkActivity extends BaseTitleActivity {

    private EditText editText;
    private TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account_remark);
        setCenterTitle("账目备注");
        setRightTitle("保存");
        mAiJuTitleBarView.setRightOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goBackForResult();
            }
        });

        editText = (EditText) findViewById(R.id.ev_account_remark);
        textView = (TextView) findViewById(R.id.tv_remark_size);
        if(getIntent().hasExtra("data")){
            editText.setText(getIntent().getStringExtra("data"));
            editText.setSelection(editText.getText().length());
            textView.setText(editText.getText().length() + "/50");
        }
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                textView.setText(s.toString().length() + "/50");
            }
        });
    }

    @Override
    public void onBackPressed() {
        goBackForResult();
    }

    /**
     * 带返回值的后退
     */
    private void goBackForResult() {
        Intent intent = new Intent();
        intent.putExtra("remark", editText.getText().toString());
        setResult(0, intent);
        finish();
    }
}
